<?php

namespace App\Admin\Controllers;

use App\Models\Term;

use Encore\Admin\Form;
use Encore\Admin\Grid;
use Encore\Admin\Facades\Admin;
use Encore\Admin\Layout\Content;
use App\Http\Controllers\Controller;
use Encore\Admin\Controllers\ModelForm;

class TermController extends Controller
{
    use ModelForm;

    /**
     * Index interface.
     *
     * @return Content
     */
    public function index()
    {
        return Admin::content(function (Content $content) {

            $content->header(trans('admin.term'));
            $content->description(trans('admin.list'));
            
            $content->body($this->grid()->render());
        });
    }

    /**
     * Edit interface.
     *
     * @param $id
     * @return Content
     */
    public function edit($id)
    {
        return Admin::content(function (Content $content) use ($id) {

            $content->header(trans('admin.term'));
            $content->description(trans('admin.edit'));

            $content->body($this->form()->edit($id));
        });
    }

    /**
     * Create interface.
     *
     * @return Content
     */
    public function create()
    {
        return Admin::content(function (Content $content) {

            $content->header(trans('admin.term'));
            $content->description(trans('admin.create'));

            $content->body($this->form());
        });
    }

    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        return Admin::grid(Term::class, function (Grid $grid) {
            $grid->model()->orderBy('id', 'desc');
            $grid->id(trans('admin.ID'))->sortable();
            $grid->name(trans('admin.name'));
            $grid->year(trans('admin.year'))->display(function () {
                    return $this->year.'学年';
            })->sortable();
            
            $grid->season(trans('admin.season'))->display(function () {
                if ($this->season==1) {
                    return trans('admin.spring').'学期';
                }elseif ($this->season==2) {
                    return trans('admin.autumn').'学期';
                }
            })->sortable();
            $grid->start_date(trans('admin.term_start_date'));
            $grid->status(trans('admin.status'))->editable('select', [0 => '启用', 1 => '禁用']);
            
            $grid->actions(function ($actions) {
                $actions->disableView();
            });

            //禁用导出按钮
            $grid->disableExport();
            //禁用筛选按钮
            $grid->disableFilter();

            $grid->tools(function ($tools) { 
                //关闭批量删除 
                $tools->batch(function ($batch) { 
                    $batch->disableDelete(); 
                });
            });
        });
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        return Admin::form(Term::class, function (Form $form) {
            $form->display('id', 'ID');
            $form->date('year', trans('admin.year'))->format('YYYY')->rules('required')->addElementLabelClass('must-input');
            $form->select('season', trans('admin.season'))->options([1 => trans('admin.spring'), 2 => trans('admin.autumn')])->rules('required')->addElementLabelClass('must-input');

            $form->date('start_date', trans('admin.term_start_date'))->format('YYYY-MM-DD')->rules('required')->addElementLabelClass('must-input');
            $form->radio('status', trans('admin.status'))->options([0 => trans('admin.enable'), 1 => trans('admin.disable')])->default(0);
            
                        //保存后回调
            $form->saved(function (Form $form) {
                if ($form->season==1) {
                    $form->season = '春季学期';
                }elseif($form->season==2){
                    $form->season = '秋季学期';
                }
                $name = $form->year.'学年'.$form->season;
                $max_id = Term::max('id');
                $term = Term::find($max_id);
                $term->name = $name;
                $term->save();
            });
            
            //去掉添加/编辑表单的重置按钮
            $form->disableReset();
            $form->tools(function (Form\Tools $tools) {
                // 去掉`列表`按钮
                $tools->disableList();
                // 去掉`删除`按钮
                $tools->disableDelete();
                // 去掉`查看`按钮
                $tools->disableView();
            });
        });
    }
}
